﻿using UnityEngine;
using static UnityEngine.Mathf;

namespace Dan{
	public static class FunctionLibrary {
	
		public delegate Vector3 Function(float u , float v, float t); //委托
		public enum FunctionName {Wave,MultiWave,Ripple,Cylinder,Sphere,Torus}; //枚举方法名
		static Function[] functions = {Wave,MultiWave,Ripple,Cylinder,Sphere,Torus}; //给方法创建数组
		
		//获取方法
		public static Function GetFunction (FunctionName name){
			return functions[(int)name];
		}

	//正弦
		public static Vector3 Wave (float u, float v,float t){
			Vector3 p;
			p.x = u;
			p.y = Sin(PI*(u+v+t));
			p.z = v;
			return p;
		}
	//叠加正弦
		public static Vector3 MultiWave(float u, float v,float t){
			Vector3 p;
			p.x = u;
			p.y = Sin(PI*(u+0.5f*t));
			p.y += 0.5f * Sin(2f*PI*(v+2f*t));//-1.5 ~ 1.5
			p.y += Sin(PI * (u + v + 0.25f * t ));
			p.y *= 1f/2.5f;
			p.z = v;
			return p ;

			
		}
	//滴落
		public static Vector3 Ripple(float u, float v,float t){
			float d = Sqrt(u*u+v*v);
			Vector3 p;
			p.x = u;
			p.y = Sin(PI *(4f *d -t));
			p.y /= (1f + 10f*d);
			p.z =v ;
			return p;
		}
	
		public static Vector3 Cylinder(float u, float v,float t){
			float r  = 0.8f + Sin(PI*(6f*u + 2f*v + t))*0.2f;
			Vector3 p;
			p.x = r*Sin(PI*u);
			p.y = v;
			p.z = r*Cos(PI*u);
			return p;
		}


	//球形
		public static Vector3 Sphere(float u, float v,float t){
			float r = 0.8f + Sin(PI*(6f*u  + t))*0.1f;
			r += Sin(PI *(4f*v + t))*0.1f;
			Vector3 p;
			float s  = r * Cos(0.5f*PI*v);
			p.x = s*Sin(PI*u);
			p.y = r*Sin(0.5f*PI*v);
			p.z = s*Cos(PI*u);
			return p;
		}
		public static Vector3 Torus(float u, float v,float t){
			float r1 = 0.65f + Sin(PI *(6f*u + t))*0.1f;
			float r2 = 0.2f + Sin(PI *(4f*v + t))*0.1f;

			Vector3 p;
			float s  =  r2 * Cos(PI*v) +r1;
			p.x = s*Sin(PI*u);
			p.y = r2 * Sin(PI*v);
			p.z = s*Cos(PI*u);
			return p;
		}
	}

}

